#include "hc32_ddl.h"
#include "bsp_clk.h"
#include "bsp_include.h"
#include "AT24C02.h"
#include "bsp_i2c.h"
#include "SEGGER_RTT.h"
#include "bsp_rmu.h"
#include "bsp_log.h"
#include "TCA9539.h"
#include <rtthread.h> 
stc_clk_freq_t Clkdata;
uint8_t data[10];
uint32_t resetFlag;
#define TIME (0x1000000u)
#define  DELAY_MS         (RT_TICK_PER_SECOND / 20)    /* 50ms */
char str[100];
void cacheon(void)
{
    EFM_Unlock();
    EFM_DataCacheCmd(Enable);
    EFM_InsCacheCmd(Enable);
    EFM_PrefetchCmd(Enable);
    EFM_Lock();
}
void system_init(void)
{
    LOG_INFO("\r\n--------system initialed -------\r\n");
    resetFlag = M4_RMU->RSTF0;
    bsp_show_resetcause(resetFlag);
    PWC_Unlock(PWC_UNLOCK_CODE_0 | PWC_UNLOCK_CODE_1 | PWC_UNLOCK_CODE_2);
    RMU_ClrStatus();    
    PWC_Lock(PWC_UNLOCK_CODE_0 | PWC_UNLOCK_CODE_1 | PWC_UNLOCK_CODE_2);	
    bsp_sys_clk_init();
    CLK_GetClockFreq(&Clkdata);
    bsp_uart1_init();
    SysTick_Init(1000);
	NVIC_EnableIRQ(SysTick_IRQn);   
    rt_kprintf("system initialed \r\n");
    LOGWARNING("resetcode: 0x%x",resetFlag);
    LOG("system colock freq %dMHZ",Clkdata.hclkFreq/1000000);
    LOG_INFO("turn on cache");
    cacheon();
    bsp_get_chipinfo();
    LOG_INFO("start RT_Thread");
}
int main(void)
{
    bsp_i2c_init(I2C1_UNIT,100000);
	TCA9539_CFG(0,0);
    ledcontrol(0x00);
	while(1)
	{
		rt_thread_delay(DELAY_MS);
	}
}
